import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2022/2/20
 * Time: 8:16
 * Description:
 */
public class TestDemo {

    public static void main(String[] args) {
//        int[][] array = new int[][3]; 不能省略行
        int[][] array = new int[2][];//可以省略列
        array[0] = new int[3];
        array[1] = new int[7];

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }

    public static void main19(String[] args) {
        int[][] array1 = {{1,2,3},{4,5,6}};
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("===========");
        System.out.println(Arrays.deepToString(array1));
    }

    public static void main18(String[] args) {
        int[][] array1 = {{1,2,3},{4,5,6}};
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("=============");
        for (int[] tmp : array1) {
            for (int x : tmp) {
                System.out.print(x+" ");
            }
            System.out.println();
        }
        int[] array3 = {1,2,3,4};
        for (int x:array3
             ) {

        }
    }

    public static void main17(String[] args) {
        int[][] array1 = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[][]{{1,2,3},{4,5,6}};
        int[][] array3 = new int[2][3];//默认都是0
    }

    public static void bubbleSort(int[] array) {
        //i表示趟数
        for (int i = 0; i < array.length-1; i++) {
            //j每一趟都是在
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if(flg == false) {
                break;
            }
        }
    }

    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length-1;
        while(left < right) {
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }

    public static void main16(String[] args) {
        int[] array = {10,8,3,9,7};
        reverse(array);
        System.out.println(Arrays.toString(array));
        /*System.out.println("排序前："+Arrays.toString(array));
        bubbleSort(array);
        System.out.println("排序后："+Arrays.toString(array));*/
    }

    public static double avg(int[] array) {
        int sum = 0;
        for (int x:
             array) {
            sum += x;
        }
        return sum*1.0/array.length;
    }

    public static int findVal(int[] array,int key) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return i;
            }
        }
        return -1;//没有关键字key
    }
    public static int count = 0;
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            count++;
            int mid = (left+right)>>>1;
            if(array[mid] == key) {
                return mid;
            }else if(array[mid] > key) {
                right = mid-1;
            }else {
                left = mid+1;
            }
        }
        return -1;
    }

    public static void main13(String[] args) {
        int[] array = new int[10000];
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
        System.out.println(binarySearch(array,9999));
        System.out.println(count);
    }

    public static void main12(String[] args) {
        int[] array = {1,2,31,4};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
        int index = Arrays.binarySearch(array,4);
        System.out.println(index);

       /* int[] ret = new int[10];
        System.out.println(Arrays.toString(ret));
        //填充
        Arrays.fill(ret,1,3,-1);
        System.out.println(Arrays.toString(ret));*/

        //System.out.println(binarySearch(array,4));
    }

    public static void main11(String[] args) {
        int[] array1 = {1,2,3,4};
        int[] array2 = array1.clone();
        System.out.println("拷贝结束，修改array2[0]");
        array2[0] = 199;
        System.out.println("array1:"+Arrays.toString(array1));
        System.out.println("array2:"+Arrays.toString(array2));
    }

    public static void main10(String[] args) {
        int[] array1 = {1,2,3,4};
        int[] array2 = new int[array1.length];
        System.arraycopy(array1,0,array2,0,array1.length);
        System.out.println("拷贝结束，修改array2[0]");
        array2[0] = 199;
        System.out.println("array1:"+Arrays.toString(array1));
        System.out.println("array2:"+Arrays.toString(array2));
    }

    public static void main9(String[] args) {
        int[] array1 = {1,2,3,4};
        //1、原来的数组    2、你要拷贝的长度
        int[] array2 = Arrays.copyOf(array1,array1.length);
        System.out.println("拷贝结束，修改array2[0]");
        array2[0] = 199;
        System.out.println("array1:"+Arrays.toString(array1));
        System.out.println("array2:"+Arrays.toString(array2));

        //拷贝from下标到to下标[form,to)
        int[] array3 = Arrays.copyOfRange(array1,1,3);//[)
        System.out.println("array3:"+Arrays.toString(array3));

    }

    public static void main8(String[] args) {
        int[] array1 = {1,2,3,4};
        //System.out.println(Arrays.toString(array1));
        int[] array3 = new int[array1.length];
        for (int i = 0; i < array1.length; i++) {
            array3[i] = array1[i];
        }
        System.out.println("拷贝结束，修改array3[0]");
        array3[0] = 199;
        System.out.println("array1:"+Arrays.toString(array1));
        System.out.println("array3:"+Arrays.toString(array3));
    }


    public static void main7(String[] args) {
        int[] array1 = {1,2,3,4};
        System.out.println(Arrays.toString(array1));
        int[] array2 = array1;
        System.out.println(Arrays.toString(array2));
    }


    public static String myToString(int[] array) {
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];//不建议在循环里面用String进行拼接，
            // 最好的方式是使用StringBuilder或者StringBuffer
            if(i != array.length-1) {
                ret += ",";
            }
        }
        ret+="]";
        return ret;
    }

    public static void main6(String[] args) {
        int[] array = {1,2,3,4};
        String ret = myToString(array);
        System.out.println(ret);//[1,2,3,4]
    }

    public static void func1(int a) {
        a = 10;
    }
    public static void func2(int[] array){
        array = new int[]{1,2,3,4};
    }
    public static void func3(int[] array) {
        array[0] = 10;
    }

    public static int[] func4() {
        int[] array = {1,2,3,4};
        return array;
    }

    public static void main5(String[] args) {
        int[] ret = func4();
        System.out.println(Arrays.toString(ret));
    }

    public static void main4(String[] args) {
        int[] arr = {9,8,7};
        func3(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void main3(String[] args) {
        int[] array = null;
        //System.out.println(array.length);
        System.out.println(array[0]);
    }

    public static void main2(String[] args) {
        int a=0;
        System.out.println(a);
        int[] array = null;
        System.out.println(array);
        int[] array2 = {1,2,3,4};
        System.out.println(array2);
    }

    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        int[] array = {1,2,3,4};
    }
}
